<?php
/*
 * Copyright 2008 by Aaron Jarzombek, Brice Rader, Dan Pepin, Michael Tierny
 * and Steve Simard.  This program is part of the University of New Hampshire
 * at Manchester Internship Program.
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 */

/**
 * Initializes the database by creating the tables:
 * dbCategory, dbContact, dbInternship, dbMajor, dbOpportunity,
 * dbOrganization, dbStudent, and dbUser
 * @version March 4, 2011
 * @author Aaron Jarzombek, Brice Rader and Daniel Pepin
 */
?>

<html>
    <title>
        Database Initialization
    </title>
    <body>
        <?php
        echo ("Attempting to install tables...<br />");
        include('inc/dbInfo.php');
        include('inc/functions.php');
        include('dbOrganization.php');
        include('dbCategory.php');
        include('dbContact.php');
        include('dbStudent.php');
        include('dbOpportunity.php');
        include('dbMajor.php');
        include('dbInternship.php');
        include('dbUser.php');

        $db_server = mysql_connect($host, $username, $password);
        if (!$db_server

            )die("Cannot connect to the MySQL database!<br />Please
            contact your database administrator.");

        mysql_query("DROP DATABASE IF EXISTS i2c");
        mysql_query("CREATE DATABASE i2c");
		
		 $db_selected = mysql_select_db($database, $db_server);
        if (!$db_selected) {
            die("Cannot select database $database." . mysql_error());
        };

        mysql_select_db($database) or die("Cannot select the database!<br />
            Please make sure it has been installed properly!");

		mysql_query("DROP USER 'i2c'@'%'");
		mysql_query("DROP USER 'i2c'@'localhost'");
		mysql_query("CREATE USER 'i2c'@'%' IDENTIFIED BY 'i2c'");
		mysql_query("CREATE USER 'i2c'@'localhost' IDENTIFIED BY 'i2c'");
		mysql_query("GRANT ALL privileges ON i2c.* TO 'i2c'@'%' IDENTIFIED BY 'i2c' with grant option");
		mysql_query("GRANT ALL privileges ON i2c.* TO 'i2c'@'localhost' IDENTIFIED BY 'i2c' with
grant option");
	


			
        //ORGANIZATION
        $result = dbOrganization::createTable();
        if (!$result

            )die("Could not create table 'organization': " . mysql_error());
        echo ("Added table 'organization' successfully...<br />");

        //CATEGORY
        $result = dbCategory::createTable();
        if (!$result

            )die("Could not create table 'category': " . mysql_error());
        echo ("Added table 'category' successfully...<br />");

        //CONTACT
        $result = dbContact::createTable();
        if (!$result

            )die("Could not create table 'contact': " . mysql_error());
        echo ("Added table 'contact' successfully...<br />");

        //STUDENT
        $result = dbStudent::createTable();
        if (!$result

            )die("Could not create table 'student': " . mysql_error());
        echo ("Added table 'student' successfully...<br />");

        //OPPORTUNITY
        $result = dbOpportunity::createTable();
        if (!$result

            )die("Could not create table 'opportunity': " . mysql_error());
        echo ("Added table 'opportunity' successfully...<br />");

        //MAJOR
        $result = dbMajor::createTable();
        if (!$result

            )die("Could not create table 'major': " . mysql_error());
        echo ("Added table 'major' successfully...<br />");

        //INTERNSHIP
        $result = dbInternship::createTable();
        if (!$result

            )die("Could not create table 'internship': " . mysql_error());
        echo ("Added table 'contact' successfully...<br />");


        //SYSUSER
        $result = dbUser::createTable();
        if (!$result

            )die("Could not create table 'sysuser': " . mysql_error());
        echo ("Added table 'sysuser' successfully...<br />");
        echo("Installation of mysql tables complete.");
        ?>
    </body>
</html>